package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Orders;
import com.ruoyi.system.domain.User;
import com.ruoyi.system.domain.vo.CommentVo;
import com.ruoyi.system.domain.vo.OrderInfo;
import com.ruoyi.system.mapper.OrdersMapper;
import com.ruoyi.system.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.OrderCommentMapper;
import com.ruoyi.system.domain.OrderComment;
import com.ruoyi.system.service.IOrderCommentService;

/**
 * 订单--评论Service业务层处理
 * 
 * @author Wxl
 * @date 2022-03-28
 */
@Service
public class OrderCommentServiceImpl implements IOrderCommentService 
{
    @Autowired
    private OrderCommentMapper orderCommentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    /**
     * 查询订单--评论
     * 
     * @param id 订单--评论主键
     * @return 订单--评论
     */
    @Override
    public OrderComment selectOrderCommentById(Long id)
    {
        return orderCommentMapper.selectOrderCommentById(id);
    }

    /**
     * 查询订单--评论列表
     * 
     * @param orderComment 订单--评论
     * @return 订单--评论
     */
    @Override
    public List<OrderComment> selectOrderCommentList(OrderComment orderComment)
    {
        return orderCommentMapper.selectOrderCommentList(orderComment);
    }

    /**
     * 新增订单--评论
     * 
     * @param orderComment 订单--评论
     * @return 结果
     */
    @Override
    public int insertOrderComment(OrderComment orderComment)
    {
        orderComment.setCreateTime(DateUtils.getNowDate());
        return orderCommentMapper.insertOrderComment(orderComment);
    }

    /**
     * 修改订单--评论
     * 
     * @param orderComment 订单--评论
     * @return 结果
     */
    @Override
    public int updateOrderComment(OrderComment orderComment)
    {
        return orderCommentMapper.updateOrderComment(orderComment);
    }

    /**
     * 批量删除订单--评论
     * 
     * @param ids 需要删除的订单--评论主键
     * @return 结果
     */
    @Override
    public int deleteOrderCommentByIds(Long[] ids)
    {
        return orderCommentMapper.deleteOrderCommentByIds(ids);
    }

    /**
     * 删除订单--评论信息
     * 
     * @param id 订单--评论主键
     * @return 结果
     */
    @Override
    public int deleteOrderCommentById(Long id)
    {
        return orderCommentMapper.deleteOrderCommentById(id);
    }

    @Override
    public int insertCommentVo(CommentVo vo) {
        OrderComment orderComment = new OrderComment();
        orderComment.setOrderNo(vo.getOrderNo());
        orderComment.setContent(vo.getInput());
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("id", vo.getUserId()));
        Orders order_no = ordersMapper.selectOne(new QueryWrapper<Orders>().eq("order_no", vo.getOrderNo()));
        order_no.setOrderStatus(3);
        orderComment.setUserName(user.getUsername());
        orderComment.setShopId(order_no.getShopId());
        orderComment.setHeadImg(user.getAvatar());
        orderComment.setCreateTime(new Date());
        orderComment.setRate(vo.getSlider());
        ordersMapper.updateById(order_no);
        int insert = orderCommentMapper.insert(orderComment);
        return insert;
    }

    @Override
    public List<OrderComment> getComment(Long shopId) {
        List<OrderComment> comments = orderCommentMapper.selectList(new QueryWrapper<OrderComment>().eq("shop_id", shopId));
        return comments;
    }
}
